#python -m data.api_data.api_history   文件直接执行代码
from .api_token import get_id,get_time,get_token
import requests
import json
with open(r'data\json_data\monitor.json','r',encoding='utf-8') as f:
    data = json.load(f)['api']

def get_headtitle():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/realtime/device'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'device_id': 57714}
    response = requests.post(url=url,headers=headers,json=json)
    for alarm in response.json()['data']['alarm_infos']:
        for key in data['devicesate']:
            if key == 'hase_alarm':
                if alarm[key] == 0:                
                    values.append('正常')
                elif alarm[key] == 1:
                    values.append('异常')
            else:
                values.append(alarm[key])
    return values
#获取历史数据
def get_historydata():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/data/history'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'device_id': 57714,'limit':5,'page':1,'start':int(get_time())*1000-604800000,'end':int(get_time())*1000}#减去一周时间
    response = requests.post(url=url,headers=headers,json=json)

    humid = []
    temp = []
    CollectTime = []
    data_list = response.json()['data']['list']
    values = []
    for item in data_list:
        for key, value in item.items():  # 直接遍历键值对
            if key.startswith('Humid'):
                humid.append(value)
            elif key.startswith('Temp'):
                temp.append(value)
            elif key == 'CollectTime':
                CollectTime.append(value)
        
        values = values+CollectTime+temp+humid
        humid.clear()
        temp.clear()
        CollectTime.clear()
    return sorted(values)


